#include "port.h"

// #define FirstRow        (0x70)
// #define SecondRow       (0xB0)
// #define ThirdRow        (0xD0)
// #define FourthRow       (0xE0)
// #define FirstColumn     (0x07)
// #define SecondColumn    (0x0B)
// #define ThirdColumn     (0x0D)
// #define FourthColumn    (0x0E)


uint8_t key_matrix_scan(void)
{
    /*
    P17 through P14 correspond to rows 1 through 4
    P13 through P10 correspond to columns 1 through 4
    P17 ---- P10
    0000 0000
    */
    uint8_t FirstRow        =0x7F;//0111 1111
    uint8_t SecondRow       =0xBF;//1011 1111
    uint8_t ThirdRow        =0xDF;//1101 1111
    uint8_t FourthRow       =0xEF;//1110 1111
    uint8_t FirstColumn     =0xF7;//1111 0111
    uint8_t SecondColumn    =0xFB;//1111 1011
    uint8_t ThirdColumn     =0xFD;//1111 1101
    uint8_t FourthColumn    =0xFE;//1111 1110
    uint8_t value = 0;


    KeyMatrix = FirstColumn;
    if(KeyMatrix != FirstColumn)
    {
        delay_ms(10);
        switch (KeyMatrix)
        {
            case 0x77:value = 1;    break;//first row, first column     0111 0111
            case 0xB7:value = 1+4;  break;//second row, first column    1011 0111
            case 0xD7:value = 1+8;  break;//third row, first column     1101 0111
            case 0xE7:value = 1+12; break;//fourth row, first column    1110 1111
            default:break;
        }
    }
    while(KeyMatrix != FirstColumn);

    KeyMatrix = SecondColumn;
    
    if(KeyMatrix != SecondColumn)
    {
        delay_ms(10);
        switch (KeyMatrix)
        {
            case 0x7B:value = 2;    break;//first row, third column     0111 1011
            case 0xBB:value = 2+4;  break;//second row, third column    1011 1011
            case 0xDB:value = 2+8;  break;//third row, third column     1101 1011
            case 0xEB:value = 2+12; break;//fourth row, third column    1110 1111
            default:break;
        }
    }
    while(KeyMatrix != SecondColumn);

    KeyMatrix = ThirdColumn;
    if(KeyMatrix != ThirdColumn)
    {
        delay_ms(10);
        switch (KeyMatrix)
        {
            case 0x7D:value = 3;    break;//first row, first column     0111 1101
            case 0xBD:value = 3+4;  break;//second row, first column    1011 1101
            case 0xDD:value = 3+8;  break;//third row, first column     1101 1101
            case 0xED:value = 3+12; break;//fourth row, first column    1110 1111
            default:break;
        }
    }
    while(KeyMatrix != ThirdColumn);

    KeyMatrix = FourthColumn;
    if(KeyMatrix != FourthColumn)
    {
        delay_ms(10);
        switch (KeyMatrix)
        {
            case 0x7E:value = 4;    break;//first row, fourth column    0111 1110
            case 0xBE:value = 4+4;  break;//second row, fourth column   1011 1110
            case 0xDE:value = 4+8;  break;//third row, fourth column    1101 1110
            case 0xEE:value = 4+12; break;//fourth row, fourth column   1110 1110
            default:break;
        }
    }
    while(KeyMatrix != FourthColumn);

    return value;
}

uint8_t key_matrix_flip(void)
{
    // static uint8_t value = 0;
    uint8_t value = 0;
    uint8_t allRow = 0x0F;
    uint8_t allColumn = 0xF0;
    KeyMatrix = allRow;
    if(KeyMatrix != allRow)//There are keys pressed
    {
        //flip row
        delay_ms(10);
        switch (KeyMatrix)//check column
        {
            case 0x07: value = 1; break;    //0000 0111
            case 0x0B: value = 2; break;    //0000 1011
            case 0x0D: value = 3; break;    //0000 1101
            case 0x0E: value = 4; break;    //0000 1110
        
            default: break;
        }
        //flip column
        KeyMatrix = allColumn;
        switch (KeyMatrix)//check row
        {
            case 0x70: value = value; break;    //0111
            case 0xB0: value = value+4; break;  //1011
            case 0xD0: value = value+8; break;  //1101
            case 0xE0: value = value+12; break; //1110


            // case 0x70:
            // case 0xB0:
            // case 0xD0: 
            // case 0xE0: value = 5*value-4; break;
        
            default: break;
        }
        while(KeyMatrix != allColumn);
    }
    else{
        // value = 0xf;
    }

    return value;
}

void key_matrix_check_flip(void)
{
    uint8_t value = 0;
    value = key_matrix_flip();

    if(value)
    {
        nixie_tube_choose(0x1, value-1);
    }
}

void key_matrix_check_scan(void)
{
    uint8_t value = 0;
    value = key_matrix_scan();

    if(value)
    {
        nixie_tube_choose(0x1, value-1);
        // beep_control(4);
    }
}